package com.bumblebee.server;
/**
 * 
 */
import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableJpaRepositories
@EnableTransactionManagement
public class AppConfiguration {

   
    public DataSource dataSource() {
    	DriverManagerDataSource datasource = new DriverManagerDataSource();
    	datasource.setDriverClassName("com.mysql.jdbc.Driver");
    	datasource.setPassword("root");
    	datasource.setUrl("jdbc:mysql://localhost/bumble_db");
    	datasource.setUsername("root");
    	return datasource;
        //EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
       //return builder.setType(EmbeddedDatabaseType.HSQL).build();
    }

   @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
       // vendorAdapter.setDatabase(Database.HSQL);
        vendorAdapter.setDatabase(Database.MYSQL);
        vendorAdapter.setGenerateDdl(true);
        vendorAdapter.setShowSql(true);
       vendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQL5Dialect");
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan(getClass().getPackage().getName());
        DriverManagerDataSource datasource = new DriverManagerDataSource();
    	datasource.setDriverClassName("com.mysql.jdbc.Driver");
    	datasource.setPassword("root");
    	datasource.setUrl("jdbc:mysql://localhost/bumble_db");
    	datasource.setUsername("root");
        factory.setDataSource(datasource);

        return factory;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new JpaTransactionManager();
    }
}
